package com.geezdata.cps.sdk3rd.sms.provider;

import java.util.Map;

import com.geezdata.cps.base.utils.HttpClientUtil;
import com.geezdata.cps.sdk3rd.sms.request.BalanceRequest;
import com.geezdata.cps.sdk3rd.sms.request.SmsRequest;
import com.geezdata.cps.sdk3rd.sms.response.SmsResult;

public abstract class BaseProvider{
	public SmsResult sendSms(SmsRequest request) {
		String rspStr = doRequest(prepareSendUrl(request), prepareSendParams(request));
        return parseSendResponse(rspStr);
	}
	
	public Integer queryBalance(BalanceRequest request) {
		String rspStr = doRequest(prepareQueryUrl(request), prepareQueryParams(request));
        return parseQueryResponse(rspStr);
	}
	
	private String doRequest(String url, Map<String, Object> params) {
		try {
			boolean usePost = usePost();
			if (usePost) {
				return HttpClientUtil.post(url, params);
			} else {
				if (params == null) {
					return HttpClientUtil.get(url);
				} else {
					return HttpClientUtil.get(url, params);
				}
			}
		} catch (Exception e) {
        	return null;
        }
	}
	
	protected abstract boolean usePost();

	protected abstract String prepareSendUrl(SmsRequest request);
	protected abstract Map<String, Object> prepareSendParams(SmsRequest request);
	protected abstract SmsResult parseSendResponse(String response);
	
	protected abstract String prepareQueryUrl(BalanceRequest request);
	protected abstract Map<String, Object> prepareQueryParams(BalanceRequest request);
	protected abstract Integer parseQueryResponse(String response);	
}
